package com.pingan.haofang.searchcloud.common.auth;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.pingan.haofang.gov.sm.account.common.SessionVisitor;
import com.pingan.haofang.gov.sm.account.common.UserThreadContext;
import com.pingan.haofang.gov.sm.account.entity.enums.YesNoEnum;
import com.pingan.haofang.searchcloud.index.dto.IndexQueryResult;
import com.pingan.haofang.searchcloud.index.service.IndexService;
import com.pingan.haofang.searchcloud.indexdatasource.dto.IndexDatasourceDTO;
import com.pingan.haofang.searchcloud.indexdatasource.service.IndexDatasourceService;
import com.pingan.haofang.searchcloud.user.dto.SearchProjectDto;
import com.pingan.haofang.searchcloud.user.service.SearchProjectService;

/**
 * 数据权限校验器
 *
 * @author zhangbi
 * @date 2017/6/16
 * @since 1.0.0
 */
public class DataAuthValidator implements ConstraintValidator<ValidDataAuth, Long> {

    private static SearchProjectService searchProjectService;

    private static IndexService indexService;

    private static IndexDatasourceService indexDatasourceService;

    private AuthDataType authDataType;

    @Override
    public void initialize(ValidDataAuth constraintAnnotation) {
        this.authDataType = constraintAnnotation.dataType();
    }

    @Override
    public boolean isValid(Long value, ConstraintValidatorContext context) {
        SessionVisitor sessionVisitor = UserThreadContext.getSessionVisitor();
        if (sessionVisitor.getSessionViewDTO().getIsRoot() == YesNoEnum.YES.getValue()) {
            return true;
        }

        switch (authDataType) {
            case PROJECT_ID:
                return checkProject(value);
            case INDEX_ID:
                return checkIndex(value);
            case DATASOURCE_ID:
                return checkDatasource(value);
        }
        return false;
    }


    /**
     * 校验数据源
     *
     * @param value
     * @return
     */
    private boolean checkDatasource(Long value) {
        IndexDatasourceDTO indexDatasourceDTO = indexDatasourceService.findByIdSimple(value);
        return checkIndex(indexDatasourceDTO.getIndexId());
    }

    /**
     * 校验索引
     *
     * @param value
     * @return
     */
    private boolean checkIndex(Long value) {
        IndexQueryResult indexDTO = indexService.queryBasicInfoById(value);
        return checkProject(indexDTO.getProjectId());
    }

    /**
     * 检查项目
     *
     * @param projectId
     * @return
     */
    private boolean checkProject(Long projectId) {
        SearchProjectDto project = searchProjectService.get(projectId);

        SessionVisitor sessionVisitor = UserThreadContext.getSessionVisitor();
        
        if (project.getMasterId().equals(sessionVisitor.getAccountId())) {
            return true;
        } else {
            return false;
        }
    }

    public static void setSearchProjectService(SearchProjectService searchProjectService) {
        DataAuthValidator.searchProjectService = searchProjectService;
    }

    public static void setIndexService(IndexService indexService) {
        DataAuthValidator.indexService = indexService;
    }

    public static void setIndexDatasourceService(IndexDatasourceService indexDatasourceService) {
        DataAuthValidator.indexDatasourceService = indexDatasourceService;
    }
}
